package com.ruoyi.project.system.course.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.project.monitor.server.domain.Sys;
import com.ruoyi.project.system.course.domain.EndTime;
import com.ruoyi.project.system.course.domain.StartTime;
import com.ruoyi.project.system.course.domain.Weekday;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.course.domain.CourseArrange;
import com.ruoyi.project.system.course.service.ICourseArrangeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 课程安排 信息操作处理
 * 
 * @author Waynnn
 * @date 2019-03-29
 */
@Controller
@RequestMapping(value = "system/courseArrange")
public class CourseArrangeController extends BaseController
{
    private String prefix = "system/courseArrange";
	
	@Autowired
	private ICourseArrangeService courseArrangeService;

	@Autowired
	private IUserService userService;
	
	@RequiresPermissions("system:courseArrange:view")
	@GetMapping()
	public String courseArrange()
	{
	    return prefix + "/courseArrange";
	}
	
	/**
	 * 查询课程安排列表
	 */
	@RequiresPermissions("system:courseArrange:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(CourseArrange courseArrange)
	{
		startPage();
        List<CourseArrange> list = courseArrangeService.selectCourseArrangeList(courseArrange);

		return getDataTable(list);
	}
	

	/**
	 * 新增课程安排
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存课程安排
	 */
	@RequiresPermissions("system:courseArrange:add")
	@Log(title = "课程安排", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(CourseArrange courseArrange)
	{		
		return toAjax(courseArrangeService.insertCourseArrange(courseArrange));
	}

	/**
	 * 修改课程安排
	 */
	@GetMapping("/edit/{courseArrangeId}")
	public String edit(@PathVariable("courseArrangeId") Long courseArrangeId, ModelMap mmap)
	{
		CourseArrange courseArrange = courseArrangeService.selectCourseArrangeById(courseArrangeId);
		if (courseArrange.getTeacherId()==null){
			mmap.put("teachers", userService.selectTeacherByCourseIdForCourseArrange(courseArrange.getCourseId(),-1L));
		}else {
			mmap.put("teachers", userService.selectTeacherByCourseIdForCourseArrange(courseArrange.getCourseId(),courseArrange.getTeacherId()));
		}
		List<EndTime>endTimes=initEndTime();
		List<StartTime>startTimes=initStartTime();
		List<Weekday>weekdays=initWeekday();

		for (EndTime endTime:endTimes){
			if (endTime.getEndTime().equals(courseArrange.getEndTime())){
				endTime.setFlag(true);
			}
		}
		for (StartTime startTime:startTimes){
			if (startTime.getStartTime().equals(courseArrange.getStartTime())){
				startTime.setFlag(true);
			}
		}
		for (Weekday weekday:weekdays){
			if (weekday.getWeekday().equals(courseArrange.getWeekDay())){
				weekday.setFlag(true);
			}
		}
		mmap.put("weekDays",weekdays);
		mmap.put("startTimes",startTimes);
		mmap.put("endTimes",endTimes);
		mmap.put("courseArrange", courseArrange);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存课程安排
	 */
	@RequiresPermissions("system:courseArrange:edit")
	@Log(title = "课程安排", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(CourseArrange courseArrange)
	{

		return toAjax(courseArrangeService.updateCourseArrange(courseArrange));
	}
	
	/**
	 * 删除课程安排
	 */
	@RequiresPermissions("system:courseArrange:remove")
	@Log(title = "课程安排", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(courseArrangeService.deleteCourseArrangeByIds(ids));
	}

	private List<Weekday> initWeekday(){
		List<Weekday> weekdays=new ArrayList<>();
		weekdays.add(new Weekday("星期一"));
		weekdays.add(new Weekday("星期二"));
		weekdays.add(new Weekday("星期三"));
		weekdays.add(new Weekday("星期四"));
		weekdays.add(new Weekday("星期五"));
		weekdays.add(new Weekday("星期六"));
		weekdays.add(new Weekday("星期七"));
		weekdays.add(new Weekday("未安排"));
		return weekdays;
	}
	private List<StartTime> initStartTime(){
		List<StartTime> startTimes=new ArrayList<>();
		startTimes.add(new StartTime("第一节"));
		startTimes.add(new StartTime("第二节"));
		startTimes.add(new StartTime("第三节"));
		startTimes.add(new StartTime("第四节"));
		startTimes.add(new StartTime("第五节"));
		startTimes.add(new StartTime("第六节"));
		startTimes.add(new StartTime("第七节"));
		startTimes.add(new StartTime("第八节"));
		startTimes.add(new StartTime("第九节"));
		startTimes.add(new StartTime("第十节"));
		startTimes.add(new StartTime("未安排"));
		return startTimes;
	}
	private List<EndTime> initEndTime(){
		List<EndTime> endTimes=new ArrayList<>();
		endTimes.add(new EndTime("第一节"));
		endTimes.add(new EndTime("第二节"));
		endTimes.add(new EndTime("第三节"));
		endTimes.add(new EndTime("第四节"));
		endTimes.add(new EndTime("第五节"));
		endTimes.add(new EndTime("第六节"));
		endTimes.add(new EndTime("第七节"));
		endTimes.add(new EndTime("第八节"));
		endTimes.add(new EndTime("第九节"));
		endTimes.add(new EndTime("第十节"));
		endTimes.add(new EndTime("未安排"));
		return endTimes;
	}


}
